#include "ManageSystem.h"
#include "status.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

static AccountType GetAccountType(char* account);

status ManageSysInit(PeoManage* peoManage){

    assert(peoManage);

    if( (TeaInit(&peoManage->teaManage)==ERROR) || (StuInit(&(peoManage->stuManage))==ERROR) )
        return ERROR;

    return OK;
}

status ManageSysWriteDate(PeoManage* peoManage){

    assert(peoManage);

    int writeFlag = WRITE_FLAG;

    FILE* pf = fopen("data.bin","wb");

    if(!pf){

        perror("open");
        return ERROR;
    }

    fwrite(&writeFlag,WRITE_FLAG_SZIE,1,pf);
    fwrite(r1.account,ACCOUNT_SIZE,1,pf);
    fwrite(r1.passwd,PASSWD_SIZE,1,pf);
    fwrite(&(peoManage->teaManage.numOfTea),sizeof(peoManage->teaManage.numOfTea),1,pf);
    fwrite(&(peoManage->stuManage.numOfStu),sizeof(peoManage->stuManage.numOfStu),1,pf);

    fwrite(peoManage->teaManage.teacher,sizeof(Teacher),peoManage->teaManage.numOfTea,pf);
    fwrite(peoManage->stuManage.student,sizeof(Student),peoManage->stuManage.numOfStu,pf);

    fclose(pf);
    pf = NULL;

    return OK;
}

status ManageSysReadDate(PeoManage* peoManage,int* writeFlag){

    assert(peoManage);

    FILE* pf = fopen("data.bin","rb");

    if(!pf){

        perror("open");
        return ERROR;
    }

    fread(writeFlag,WRITE_FLAG_SZIE,1,pf);
    
    if(*writeFlag == WRITE_FLAG){

        fread(r1.account,ACCOUNT_SIZE,1,pf);
        fread(r1.passwd,PASSWD_SIZE,1,pf);
        fread(&(peoManage->teaManage.numOfTea),sizeof(peoManage->teaManage.numOfTea),1,pf);
        fread(&(peoManage->stuManage.numOfStu),sizeof(peoManage->stuManage.numOfStu),1,pf);

        Teacher* p1 = (Teacher*)malloc(peoManage->teaManage.numOfTea * sizeof(Teacher));
        if(!p1) exit(OVERFLOW);

        peoManage->teaManage.teacher = p1;
        peoManage->teaManage.capcity = peoManage->teaManage.numOfTea;

        fread(peoManage->teaManage.teacher,sizeof(Teacher),peoManage->teaManage.numOfTea,pf);

        Student* p2 = (Student*)malloc(peoManage->stuManage.numOfStu * sizeof(Student));
        if(!p2) exit(OVERFLOW);

        peoManage->stuManage.student = p2;
        peoManage->stuManage.capcity = peoManage->stuManage.numOfStu;

        fread(peoManage->stuManage.student,sizeof(Student),peoManage->stuManage.numOfStu,pf);
    }

    fclose(pf);
    pf = NULL;

    return OK;
}

void ManageSysShowLogin(){

    printf("*********************************************\n");
    printf("                欢迎来到学生系统               \n");
    printf("                1.请登录  0.退出               \n");
    printf("*********************************************\n");
}

//返回账户类型 账户下标
void ManageSysLogin(PeoManage* peoManage,AccountType* accountType,int* id,int* index){

    assert(peoManage && accountType && id && index);

    int isExist = EXIST;    //默认账号存在

    char account[ACCOUNT_SIZE];
    char passwd[PASSWD_SIZE];

    //清空account passwd
    memset(account,0,ACCOUNT_SIZE);
    memset(passwd,0,PASSWD_SIZE);

    while(1){

        printf("请输入账户(10位)>");
        scanf("%s",account);

        //判断账户类型
        *accountType = GetAccountType(account);

        if(*accountType == ERRORTYPE)
            printf("输入账户格式有误\n");
        else 
            break;
    }

    //根据账户类型,寻找下标
    if( (*accountType == TEAACCOUNT) && \
        (TeaAccountIsExist(&peoManage->teaManage,account,index,TEA_FIND) == NOTEXIST) ){

            printf("该老师账户不存在\n");
            *accountType = ERRORTYPE;
            isExist = NOTEXIST;
    }
    else if( (*accountType == STUACCOUNT) && \
             (StuAccountIsExist(&peoManage->stuManage,account,index,STU_FIND) == NOTEXIST) ){

            printf("该学生账户不存在\n");
            *accountType = ERRORTYPE;
            isExist = NOTEXIST;
    }

    if(isExist == EXIST){

        int cnt = PASSWD_INPUT_CNT;

        while(cnt--){

            memset(passwd,0,PASSWD_SIZE);
            printf("请输入密码(%d及以内)>",PASSWD_SIZE-1);
            scanf("%s",passwd);

            if( (*accountType == TEAACCOUNT) && \
                (strcmp(passwd,(peoManage->teaManage.teacher+*index)->passwd)==0) ){
                
                *id = (peoManage->teaManage.teacher+*index)->id;
                printf("老师密码正确\n");
                break;
            }
            else if( (*accountType == STUACCOUNT) && \
                     (strcmp(passwd,(peoManage->stuManage.student+*index)->passwd)==0) ){
                    
                    *id = (peoManage->stuManage.student+*index)->id;
                    printf("学生密码正确\n");
                    break;
            }
            else if( (*accountType == ROOTACCOUNT) && (strcmp(passwd,r1.passwd)==0) ){

                    printf("root密码正确\n");
                    break;
            }   

            (cnt > 0) ? (printf("密码输入错误,还有%d次机会\n",cnt)) : \
                        (printf("密码输入错误,没有机会\n"),*accountType = ERRORTYPE);
        }
    }

    PRESS_CONTINUE();
    SCREENCLEAN();

}

static AccountType GetAccountType(char* account){

    if(!strcmp(account,r1.account))
        return ROOTACCOUNT;
    else if(!strncmp(account,TEAACCOUNT_FLAG,strlen(TEAACCOUNT_FLAG)) && (strlen(account)==ACCOUNT_SIZE-1))
        return TEAACCOUNT;
    else if(!strncmp(account,STUACCOUNT_FLAG,strlen(STUACCOUNT_FLAG)) && (strlen(account)==ACCOUNT_SIZE-1))
        return STUACCOUNT;
    else
        return ERRORTYPE;
}

void ManageSysShowMenuRoot(){

    printf("*********************************************\n");
    printf("              1.打印信息  0.退出               \n");
    printf("*********************************************\n");
}

//root
void ManageSysShowAllMessage(PeoManage* peoManage){

    assert(peoManage);
    
    printf("account: %s\tpasswd: %s\n",r1.account,r1.passwd);

    TeaShowAll(&peoManage->teaManage);
    StuShowAll(&peoManage->stuManage);
}

//学生
void ManageSysShowMenuStu(){

    printf("======================欢迎进入学生端======================\n");
    printf("1.查看个人信息 2.修改信息 3.查看所有学生信息(不包括账号和密码)\n");
    printf("          4.根据id进行排序 5.根据分数排序 0.退出            \n");
    printf("======================欢迎进入学生端======================\n");
}

void ManageSysShowMyMessageStu(PeoManage* peoManage,int id){

    assert(peoManage);

    StuShowAllById(&peoManage->stuManage,id);
}

status ManageSysChangeMyMessageStu(PeoManage* peoManage,int id){

    assert(peoManage);

    return StuChangeMessageById(&peoManage->stuManage,id);
}

void ManageSysShowAllScoreStu(PeoManage* peoManage){

    assert(peoManage);

    StuShowScore(&peoManage->stuManage,1,peoManage->stuManage.numOfStu);
}

//老师
void ManageSysShowMenuTea(){

    printf("======================欢迎进入教师端======================\n");
    printf("1.查看个人信息 2.修改信息 3.查看所有老师信息(不包括账号和密码)\n");
    printf("     4.根据id对老师进行排序 5.查看学生所有信息 6.添加学生    \n");
    printf("          7.修改学生信息 8.修改学生成绩 9.删除学生          \n");
    printf("       10.根据id对学生排序 11.根据分数对学生排序 0.退出     \n");
    printf("======================欢迎进入教师端======================\n");
}
 
void ManageSysShowMyMessageTea(PeoManage* peoManage,int id){

    assert(peoManage);

    TeaShowAllById(&peoManage->teaManage,id);
}

status ManageSysChangeMyMessageTea(PeoManage* PeoManage,int id){

    assert(PeoManage);

    return TeaChangeMessageById(&PeoManage->teaManage,id);
}

void ManageSysShowAllTeaMessageTea(PeoManage* peoManage){

    assert(peoManage);

    TeaShowAllLess(&peoManage->teaManage);
}

void ManageSysSortTeaByIdTea(PeoManage* peoManage){

    assert(peoManage);

    TeaSortById(&peoManage->teaManage);
}

void ManageSysShowAllStuMessageTea(PeoManage* peoManage){

    assert(peoManage);

    StuShowAll(&peoManage->stuManage);
}

//默认尾插
status ManageSysAddStuTea(PeoManage* peoManage){

    assert(peoManage);

    return StuInsert(&peoManage->stuManage,peoManage->stuManage.numOfStu+1);
}

void ManageSysChangeStuMessageTea(PeoManage* peoManage){

    assert(peoManage);

    int id = -1;

    printf("请输入学生id>");
    scanf("%d",&id);

    status sta = StuChangeMessageById(&peoManage->stuManage,id);

    if(sta == OK) printf("修改成功\n");
    else printf("修改失败\n");
}

void ManageSysChangeStuScoreTea(PeoManage* peoManage){

    assert(peoManage);

    int id = -1;
    printf("请输入id>");
    scanf("%d",&id);

    status sta = StuChangeScoreById(&peoManage->stuManage,id);

    if(sta == OK) printf("修改成功\n");
    else printf("修改失败\n");
}

void ManageSysDelStuTea(PeoManage* peoManage){

    assert(peoManage);

    int id = -1;
    printf("请输入id>");
    scanf("%d",&id);

    status sta = StuDelById(&peoManage->stuManage,id);

    if(sta == OK) printf("删除成功\n");
    else printf("删除失败\n");
}

//公有
status ManageSysSortStuById(PeoManage* peoManage){

    assert(peoManage);

    return StuSortById(&peoManage->stuManage);
}

status ManageSysSortStuByScore(PeoManage* peoManage){

    assert(peoManage);

    return StuSortByScore(&peoManage->stuManage);
}
